/**
 * A look at List and Map "syntax sugar" in Groovy
 * Examples adapted from:
 * Davis, Scott. Groovy Recipes: Greasing the Wheels of Java. Pragmatic Bookshelf, January 2008.
 *
 * User: mstine
 * Date: Mar 18, 2008
 * Time: 11:15:22 AM
 */

//Empty List
def empty = []
println "empty.size => " + empty.size() + "\n"

//Adding an Element
def languages = ["Java", "Groovy", "Ruby"]
println "languages b4 => " + languages + "\n"
languages << "Scala"
println "languages after => " + languages + "\n"

//Getting an Element
println "languages[1] => " + languages[1] + "\n"

//Iterating
languages.each{println it}
println()

//Iterating w/ Index
languages.eachWithIndex{lang, i -> println "${i}: ${lang}"}
println()

//Sorting
languages.sort()
println "${languages}\n"

//Reverse
println "${languages.reverse()}\n"

//Pop
def top = languages.pop()
println "top => ${top}"
println "languages => ${languages}\n"

//Concatenation
def others = ["Jython", "JavaScript"]
languages += others
println "After add: ${languages}"
languages -= others
println "After remove: ${languages}\n"

//Join
println languages.join(", ") + "\n"

//Find All
println languages.findAll{it.startsWith("G")}
println()

//Max, Min, Sum
def scores = [80, 90, 70]
println "scores.max() => ${scores.max()}"
println "scores.min() => ${scores.min()}"
println "scores.sum() => ${scores.sum()}\n"

//Collect
def coolLanguages = languages.collect{it += " is cool!"}
println "${coolLanguages}\n"

//Empty Map
def emptyMap = [:]
println "emptyMap.size() => ${emptyMap.size()}\n"

//Getting an Element
def family = [dad:"Matt", mom:"Wendy"]
println "family.dad => ${family.dad}\n"

//Adding an Element
family.put("kid", "Abby")
family.kid2 = "Isabella"
family['kid3'] = "Ali Kate"
println "${family}\n"

//Iterating

//Basic
family.each{println it}
println()
//Getting key, value from it
family.each{println "${it.value} is the ${it.key}"}
println()
//Named variables for key, value
family.each{k,v -> println "${v} is the ${k}"}
println()

//Concatenation
def inLaws = [motherInLaw:"Linda",fatherInLaw:"Darrell"]
family += inLaws
println "After concat => ${family}"

inLaws.each{k,v -> family.remove("${k}")}
println "After remove => ${family}\n"

//GO BACK TO SLIDES